package Activity4;
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

public class Hangman2 {
    public static void main(String[] args) {
        Hangman2 hg = new Hangman2();
        hg.showMenu();
    }


    public void showMenu() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("------Menu------");
            System.out.println("1.Play");
            System.out.println("2.Instructions");
            System.out.println("3.Exit");
            System.out.println("\nChoose your option: ");
            int option = sc.nextInt();
            sc.nextLine();

            switch (option) {
                case 1:
                    playGame();
                    break;
                case 2:
                    readinstructions();
                    break;
                case 3:
                    System.out.println("Exiting......");
                    return;
                default:
                    System.out.println("Wrong choice!Please try again: ");
                    break;
            }
        }
    }


    public void playGame() {
        String word[] = {"japan","qatar","syria","mongolia","bahrain","india","China"};
        Random rd = new Random();
        Scanner input = new Scanner(System.in);
        String choic;

        do {
            int rnd = rd.nextInt(word.length);
            int len = word[rnd].length();
            char[] newString = new char[len];
            Arrays.fill(newString, '_'); // 初始化为下划线
            StringBuilder wrgString = new StringBuilder();
            int attempts = 0;
            int correctGuesses = 0;

            System.out.println("Pattern: "+len+" characters");
            System.out.println(newString);
            System.out.println();

            while(correctGuesses < len) {
                System.out.println("\n\nEnter your guess (single letter): ");
                String ch = input.nextLine().toLowerCase();

                // 验证输入
                if(ch.length() != 1 || !Character.isLetter(ch.charAt(0))) {
                    System.out.println("Please enter a single letter!");
                    continue;
                }

                attempts++;
                boolean found = false;

                for(int i=0; i<len; i++) {
                    if(word[rnd].charAt(i) == ch.charAt(0) && newString[i] != ch.charAt(0)) {
                        newString[i] = ch.charAt(0);
                        correctGuesses++;
                        found = true;
                    }
                }

                if(!found) {
                    if(wrgString.indexOf(ch) == -1) { // 避免重复添加错误字母
                        wrgString.append(ch).append(',');
                    }
                    System.out.println("\nMisses: "+wrgString);
                }

                System.out.println(newString);
            }

            System.out.println("------Congrats : You won in " + attempts + " attempts------");
            System.out.println("Do you want to play again? (y/n)");
            choic = input.nextLine();

        } while(choic.equalsIgnoreCase("y"));

        showMenu();
    }


        public void readinstructions() {
            System.out.println("Please read the instructions before the play.......");
        }
}

